package org.zsl.mqdemo;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.zsl.mqdemo.model.User;
import org.zsl.mqdemo.service.UserService;

import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitTestor {


    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @Test
    public void amqpAdminInitor() {

        amqpAdmin.declareExchange(new FanoutExchange("fanout_exchange")); // 创建交换机


        amqpAdmin.declareQueue(new Queue("fanout_queue_email")); //创建一个队列
        amqpAdmin.declareBinding(new Binding("fanout_queue_email",
                Binding.DestinationType.QUEUE, "fanout_exchange", "", null));

    }

    @Test
    public void testProducer0() {


        rabbitTemplate.convertAndSend("");
    }



    @Test
    public void testProducer1() {


        for (int i=1;i<10;i++){
            User user = new User();
            user.setUsername("test"+i);

            rabbitTemplate.convertAndSend("fanout_exchange", "", user);
        }



    }

    @Test
    public void testProducer2() {

        Map<String,Object> map = new HashMap<>();
        map.put("username","admin");

        rabbitTemplate.convertAndSend("fanout_exchange", "", map);

    }


    @Test
    public void testComsumer1() throws InterruptedException {
        Object o = rabbitTemplate.receiveAndConvert("fanout_queue_email");
        System.out.println(o.getClass());

        System.out.println("work1:"+o);

        Thread.sleep(2000);

        Object o2 = rabbitTemplate.receiveAndConvert("fanout_queue_email");
        System.out.println(o2.getClass());

        System.out.println("work2:"+o2);
    }



}


